package com.jr.controller;


import com.google.gson.Gson;
import com.jr.entity.UserJobEnterprise;
import com.jr.service.UserJobEnterpriseService;
import com.jr.util.DateCalculation;
import com.jr.util.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 @author Summer
 @email 2838259857@qq.com
 */
@Controller


public class UserJobEnterpriseController {

    @Autowired
    private UserJobEnterpriseService userJobEnterpriseService;

    /**
     * 获得用户关注工作列表分页
     */
    @RequestMapping(value = "getjobsByUidPageHelper.do", produces = "text/html;charset=utf-8")
    public @ResponseBody String getjobsByUidPageHelper(PageHelper<UserJobEnterprise> pageHelper) {
        System.out.println(pageHelper);
        pageHelper.setCountnums(userJobEnterpriseService.countNumbersOfJobsByUid(pageHelper));
        pageHelper.setCountpage();
        pageHelper.setUserJobEnterpriseDaoList(userJobEnterpriseService.queryJobAllByUid(pageHelper));

        return new Gson().toJson(pageHelper);
    }

    /**
     * 获得关键字查询的工作列表
     */
    @RequestMapping(value = "getjobsByKeywordPageHelper.do", produces = "text/html;charset=utf-8")
    public @ResponseBody String getjobsByKeywordPageHelper(PageHelper<UserJobEnterprise> pageHelper) {
        pageHelper.setCountnums(userJobEnterpriseService.countNumbersOfJobs(pageHelper));
        pageHelper.setCountpage();
        pageHelper.setUserJobEnterpriseDaoList(userJobEnterpriseService.queryJobAll(pageHelper));

        return new Gson().toJson(pageHelper);
    }

    //getOnejobByJid.do?uid=1&jid=1
    /**
     * 获得单个工作的详细信息
     */
    @RequestMapping(value = "getOnejobByJid.do", produces = "text/html;charset=utf-8")
    public ModelAndView getOnejobByJid(PageHelper<UserJobEnterprise> pageHelper) {
        UserJobEnterprise userJobEnterprise = userJobEnterpriseService.selectOneJobWithUid(pageHelper);
        Date jreleasedate = userJobEnterprise.getJob().getJreleasedate();
        Date jrefreshdate = userJobEnterprise.getJob().getJrefreshdate();
        String datePoor = DateCalculation.getDatePoor(jrefreshdate, jreleasedate);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userJobEnterprise", userJobEnterprise);
        modelAndView.addObject("datePoor", datePoor);
        modelAndView.setViewName("job_info.jsp");
        return modelAndView;
    }

    //getjobsByEid.do?eid=1
    /**
     * 获得企业发布的招聘信息，使用工具类但未进行分页
     */
    @RequestMapping(value = "getjobsByEid.do", produces = "text/html;charset=utf-8")
    public @ResponseBody String getjobsByEid(PageHelper<UserJobEnterprise> pageHelper) {
        pageHelper.setUserJobEnterpriseDaoList(userJobEnterpriseService.queryJobsByEid(pageHelper));

        return new Gson().toJson(pageHelper);
    }

    /**
     * 添加用户关注的工作
     */
    @RequestMapping(value = "addUserJobEnterprisewithUser.do", produces = "text/html;charset=utf-8")
    public @ResponseBody String addUserJobEnterprisewithUser(UserJobEnterprise userJobEnterprise) {
        int i = userJobEnterpriseService.addUserJobEnterprise(userJobEnterprise);
        return new Gson().toJson(i);
    }
    /**
     * 查找用户关注的工作
     */
    @RequestMapping(value = "queryUserJobEnterprise.do", produces = "text/html;charset=utf-8")
    public @ResponseBody String queryUserJobEnterprise(UserJobEnterprise userJobEnterprise) {
        UserJobEnterprise userJobEnterprise1 = userJobEnterpriseService.queryUserJobEnterprise(userJobEnterprise);
        System.out.println(userJobEnterprise1);
        return new Gson().toJson(userJobEnterprise1);
    }
    /**
     * 修改用户关注的工作，（并调用删除用户未关注和未申请的工作信息方法）
     */
    @RequestMapping(value = "modifyUserJobEnterprise.do", produces = "text/html;charset=utf-8")
    public @ResponseBody String modifyUserJobEnterprise(UserJobEnterprise userJobEnterprise) {
        int i = userJobEnterpriseService.modifuUserJobEnterprise(userJobEnterprise);
        userJobEnterpriseService.removeByJobuserstatusAndJobuserattentionWithUser();
        return new Gson().toJson(i);
    }


}